package net.ojbk.twobox.server;

import net.ojbk.twobox.pojo.TcpMessage;
import org.apache.log4j.Logger;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * PACKAGE_NAME: net.ojbk.twobox.server
 * Description:
 */
public class FileReceiveServer implements Runnable {
    public static int localPort = 29999;
    private static Logger logger = Logger.getLogger(FileReceiveServer.class);
    private String dirAdd = "/shiyi/receiveFiles/";
    //private String dirAdd = "C:\\Users\\twobox\\Desktop\\";
    private ServerSocket serverSocket;

    public FileReceiveServer() throws IOException {
        serverSocket = new ServerSocket(localPort);
        logger.info("启动服务");
    }

    public static void main(String[] args) throws IOException {
        new Thread(new FileReceiveServer()).start();
    }

    @Override
    public void run() {
        while (true) {
            try {
                final Socket socket = serverSocket.accept();
                logger.info("用户接入：" + socket.getInetAddress().getHostAddress() + ":" + socket.getPort());
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            ObjectInputStream objectInputStream = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
                            logger.debug("创建Obj输入流");
                            TcpMessage tcpMessage = (TcpMessage) objectInputStream.readObject();
                            logger.debug("收到消息：" + tcpMessage);
                            // 1 客户端想要发送文件给服务器。
                            switch (tcpMessage.code) {
                                case 1:
                                    Object[] data = (Object[]) tcpMessage.data;
                                    // 0文件名 1大小
                                    String fileName = (String) data[0];
                                    long fileLen = (long) data[1];
                                    // 创建文件输出到硬盘流
                                    OutputStream fileOut = new FileOutputStream(new File(dirAdd, fileName));
                                    byte[] bytes = new byte[1024]; // 一次接受1M字节
                                    int len = -1; //本次接收到的字节数
                                    InputStream tcpIn = socket.getInputStream();
                                    while ((len = tcpIn.read(bytes)) != -1) {
                                        fileOut.write(bytes, 0, len);
                                    }
                                    fileOut.flush();
                                    logger.debug("接受文件：" + fileName);
                                    // 关闭链接
                                    fileOut.close();
                                    tcpIn.close();
                                    logger.debug("关闭流：" + "fileOut,topIn");
                                    break;
                            }
                            objectInputStream.close();
                            logger.debug("关闭流：" + "objIn");
                            logger.debug("关闭连接：" + socket.getInetAddress().getHostAddress() + ":" + socket.getPort());
                            socket.close();
                        } catch (IOException | ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
